<?php
/* Get user info. */
global $wpdb, $current_user, $wp_locale;
$hide_right_sidebar = true;
$user_info = get_userdata($valgt_medlem);
?>

<div id="container">
    <div id="content" role="main">
        <?php if (!is_user_logged_in()) { ?>
        <p class="warning">
            <?php echo 'Du må være logget inn for å endre profilinformasjon.' ?>
        </p>
        <?php } else { 
            if (count($error) > 0) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
        <form method="post" id="adduser" action="<?php the_permalink() ?>">
            <table style="width:440px;font-size:13px;">
                <tr class="form-username">
                    <td style="border:none;">
                        <label for="first_name"><?php _e('Fornavn: ', 'profile') ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="first_name" type="text" id="first-name" value="<?php echo $user_info->first_name ?>" style="width:100%"/>
                    </td>
                </tr><!-- .form-username -->
                <tr class="form-username">
                    <td style="border:none;">
                        <label for="last_name"><?php _e('Etternavn: ', 'profile') ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="last_name" type="text" id="last-name" value="<?php echo $user_info->last_name ?>" style="width:100%"/>
                    </td>
                </tr><!-- .form-username -->
                <tr class="form-instrument">
                    <td style="border:none;">
                        <label for="instrument"><?php _e('Instrument: ', 'profile') ?></label>
                    </td>
                    <td style="border:none;">
                        <select name="instrument" style="padding:6px; border: 1px solid #ccc;border-radius: 3px; width:100%;">
                            <option value="null">Velg instrument</option>
                            <?php
                            $instrumenter = $wpdb->get_results("
                                SELECT feltdataid AS fID, feltdata
                                FROM feltdata
                                WHERE felttype = 'instrument'
                                ORDER BY indeks ASC", ARRAY_A);
                            foreach ($instrumenter as $instrument) {
                                $sel = $instrument['fID'] == $user_info->instrument
                                ?>
                            <option <?php if ($sel) { ?>selected="true" <?php } ?>value="<?php echo $instrument['fID'] ?>"><?php echo $instrument['feltdata']; ?></option>
                            <?php } ?>
                        </select>
                    </td>
                </tr>
                <?php if ($valgt_medlem == $current_user->id) { ?>
                <?php
                $fd = get_the_author_meta('fødselsdag', $current_user->id);
                $år = intval(substr($fd, 0, 4));
                $md = intval(substr($fd, 5, 2));
                $dg = intval(substr($fd, 8, 2));
                ?>
                <tr class="form-bdag">
                    <td style="border:none;">
                        <label for="bursdag"><?php _e('Fødselsdato: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <a name="bursdag">
                            <select name="fødselsdag" style="padding:6px; border: 1px solid #ccc;border-radius: 3px; width:32%;">
                                <option value="dag">dag</option>
                                <?php
                                for ($d = 1; $d < 32; $d++) {
                                    $sel = $d == $dg;
                                    ?>
                                <option <?php if ($sel) { ?>selected="true" <?php } ?>value="<?php echo $d ?>"><?php echo $d ?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <!--<input class="text-input" name="fødselsdag" type="text" id="fødselsdag" value="<?php echo $dg; ?>" style="width:30px;"/> ---> 
                            <select name="fødselsmåned" style="padding:6px; border: 1px solid #ccc;border-radius: 3px; width:32%;">
                                <option value="mnd">mnd</option>
                                <?php
                                for ($m = 1; $m < 13; $m++) {
                                    $sel = $m == $md;
                                    ?>
                                <option <?php if ($sel) { ?>selected="true" <?php } ?>value="<?php echo $m ?>"><?php echo $wp_locale->get_month_abbrev($wp_locale->get_month($m)) ?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <!--<input class="text-input" name="fødselsmåned" type="text" id="fødselsmnd" value="<?php echo $md; ?>" style="width:30px;"/> ---> 
                            <select name="fødselsår" style="padding:6px; border: 1px solid #ccc;border-radius: 3px; width:33%;">
                                <?php
                                for ($å = intval(date('Y')); $å >= 1900; $å--) {
                                    $sel = $å == $år;
                                    ?>
                                <option <?php if ($sel) { ?>selected="true" <?php } ?>value="<?php echo $å ?>"><?php echo $å?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <!--<input class="text-input" name="fødselsår" type="text" id="fødselsår" value="<?php echo $år; ?>" style="width:30px;"/>-->
                        </a>
                    </td>
                </tr>
                <tr class="form-email">
                    <td style="border:none;">
                        <label for="email"><?php _e('Epost: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta('user_email', $current_user->id); ?>" style="width:100%"/>
                    </td>
                </tr><!-- .form-email -->
                <tr class="form-tlf">
                    <td style="border:none;">
                        <label for="tlf"><?php _e('Telefon: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="telefon" type="text" id="tlf" value="<?php the_author_meta('telefon', $current_user->id); ?>" style="width:100%"/>
                    </td>
                </tr><!-- .form-tlf -->
                <tr class="form-adresse">
                    <td style="border:none;">
                        <label for="adresse"><?php _e('Adresse: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="adresse" type="text" id="adresse" value="<?php the_author_meta('adresse', $current_user->id); ?>" style="width:100%"/>
                    </td>
                </tr><!-- .form-adresse -->
                <tr class="form-postnummer">
                    <td style="border:none;">
                        <label for="postnummer"><?php _e('Postnummer: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="postnummer" type="text" id="postnummer" value="<?php the_author_meta('postnr', $current_user->id); ?>" style="width:40px;"/>
                        <a>Stedsnavn</a>
                    </td>
                </tr><!-- .form-postnummer -->
                <tr class="form-password">
                    <td style="border:none;">
                        <label for="pass1"><?php _e('Nytt passord: ', 'profile'); ?> </label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="pass1" type="password" id="pass1" style="width:100%"/>
                    </td>
                </tr><!-- .form-password -->
                <tr class="form-password">
                    <td style="border:none;">
                        <label for="pass2"><?php _e('Gjenta nytt passord: ', 'profile'); ?></label>
                    </td>
                    <td style="border:none;">
                        <input class="text-input" name="pass2" type="password" id="pass2" style="width:100%"/>
                    </td>
                </tr><!-- .form-password -->
                <?php } ?>
                <tr class="form-textarea">
                    <td colspan="2" style="border:none;">
                        <label for="description"><?php _e('Tilleggsinformasjon: ', 'profile') ?></label>
                        <textarea name="description" id="description" rows="3" cols="50" style="width:100%;"><?php echo $user_info->description ?></textarea>
                    </td>
                </tr><!-- .form-textarea -->
            </table>
            <!--instrument og tilleggsinformasjon-->
            <p class="form-submit">
                <input name="action" type="submit" id="update-this-user" class="submit button" value="Oppdater" />
                <input name="idnum" type="hidden" id="action" value="<?php echo $valgt_medlem ?>" />
                <input name="action" type="submit" value="Avbryt"/>
                <input name="til-side" type="hidden" value="profil"/>
            </p><!-- .form-submit -->
        </form><!-- #adduser -->
        <?php } ?>
    </div><!-- .entry-content -->
</div><!-- .hentry .post -->